﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using saigonbook.Dal;
using System.Data;

namespace saigonbook.Biz
{
    public class BIZ_phieunhap
    {
        public int maphieunhap;
        public string nhacungcap;
        public string diachi;
        public string sodienthoai;
        public string email;
        public int tongtien;
        public DateTime ngaynhap;

        public int Maphieunhap
        {
            get { return maphieunhap; }
            set { maphieunhap = value; }
        }
        public string Nhacungcap
        {
            get { return nhacungcap; }
            set { nhacungcap = value; }
        }
        public string Sodienthoai
        {
            get { return sodienthoai; }
            set { sodienthoai = value; }
        }
        public string Diachi
        {
            get { return diachi; }
            set { diachi = value; }
        }
        public string Email
        {
            get { return email; }
            set { email = value; }
        }
        public int Tongtien
        {
            get { return tongtien; }
            set { tongtien = value; }
        }
        public DateTime Ngaynhap
        {
            get { return ngaynhap; }
            set { ngaynhap = value; }
        }
        public List<BIZ_chitietphieunhap> Chitietphieunhap
        {
            get { return DAL_phieunhap.Select_chitietphieunhap(maphieunhap); }
        }

        public BIZ_phieunhap()
        { }
        public BIZ_phieunhap(int maphieunhap, string nhacungcap, string diachi, string sodienthoai, string email, DateTime ngaynhap, int tongtien)
        {
            this.maphieunhap = maphieunhap;
            this.nhacungcap = nhacungcap;
            this.diachi = diachi;
            this.sodienthoai = sodienthoai;
            this.email = email;
            this.tongtien = tongtien;
            this.ngaynhap = ngaynhap;
        }

        public static List<BIZ_phieunhap> Select_phieunhap()
        {
            return DAL_phieunhap.Select_phieunhap();
        }

        public static int Select_maphieu()
        {
            List<BIZ_phieunhap> ds = new List<BIZ_phieunhap>();
            ds = DAL_phieunhap.Select_phieunhap();
            ds = (from p in ds orderby p.maphieunhap descending select p).Take(1).ToList();
            foreach (BIZ_phieunhap pn in ds)
                return pn.maphieunhap + 1;
            return 1;
        }

        public void Them_chitietphieunhap(BIZ_chitietphieunhap dto)
        {
            DAL_phieunhap.Them_chitietphieunhap(dto);
        }

        public List<BIZ_chitietphieunhap> Select_chitietphieunhap()
        {
            return DAL_phieunhap.Select_chitietphieunhap();
        }

        public int soluongnhaptrongngay(int masach, DateTime ngay)
        {
            int soluong = 0;
            List<BIZ_chitietphieunhap> ds = new List<BIZ_chitietphieunhap>();
            ds = DAL_phieunhap.Select_chitietphieunhap();
            ds = (from ctpn in ds 
                  where ctpn.sach.masach == masach && ctpn.phieunhap.ngaynhap.Date == ngay.Date
                  select ctpn).ToList();
            foreach(BIZ_chitietphieunhap ct in ds)
            {
                soluong += ct.soluong;
            }
            return soluong;
        }

        public void Them_phieunhap(BIZ_phieunhap dto, List<BIZ_chitietphieunhap> dsctpn)
        {
            DAL_phieunhap.Them_phieunhap(dto);
            foreach (BIZ_chitietphieunhap ctpn in dsctpn)
            {
                Them_chitietphieunhap(ctpn);
                BIZ_tonkho tk = new BIZ_tonkho();
                tk.ngay = dto.ngaynhap.Date;
                tk.sach = new BIZ_sach();
                tk.sach.masach = ctpn.sach.masach;
                tk.soluong = tk.soluongtonkho(tk);
                tk.soluong += ctpn.soluong;
                if (tk.kiemtratontaingay(tk))
                    DAL_tonkho.Capnhat_tonkho(tk);
                else
                    DAL_tonkho.Them_tonkho(tk);
            }
        }

        bool kiemtranamnhuan(int nam)
        {
            if (nam % 4 == 0 && nam % 100 != 0)
                return true;
            return false;
        }

        int layngay(int thang, int nam)
        {
            if (thang == 2)
            {
                if (kiemtranamnhuan(nam))
                    return 29;
                else
                    return 28;
            }
            else
            {
                if (thang == 4 || thang == 6 || thang == 9 || thang == 12)
                    return 30;
                else
                    return 31;
            }
        }
        
        public void Thongketheothang_phieunhap(int thang, int nam, int[] soluongsanpham, int[] tongtien)
        {
            List<BIZ_chitietphieunhap> dsct = new List<BIZ_chitietphieunhap>();
            dsct = DAL_phieunhap.Select_chitietphieunhap();
            dsct = (from c in dsct
                    where c.phieunhap.ngaynhap.Month == thang && c.phieunhap.ngaynhap.Year == nam
                    select c).ToList();
            List<BIZ_phieunhap> ds = new List<BIZ_phieunhap>();
            ds = DAL_phieunhap.Select_phieunhap();
            ds = (from pn in ds where pn.ngaynhap.Month == thang && pn.ngaynhap.Year== nam select pn).ToList();
            for (int i = 1; i < layngay(thang, nam) + 1; i++)
            {
                soluongsanpham[i] = laysoluongsanphamtheongay(i, dsct);
                tongtien[i] = laytongtientheongay(i, ds);
            }
        }

        public int laytongtientheongay(int ngay, List<BIZ_phieunhap> ds)
        {
            int tong = 0;
            ds = (from d in ds
                  where d.ngaynhap.Day == ngay
                  select d).ToList();
            foreach (BIZ_phieunhap pn in ds)
            {
                tong += pn.tongtien;
            }
            return tong;
        }

        public int laysoluongsanphamtheongay(int ngay, List<BIZ_chitietphieunhap> dsct)
        {
            int soluong = 0;
            dsct = (from c in dsct
                    where c.phieunhap.ngaynhap.Day == ngay
                    select c).ToList();
            foreach (BIZ_chitietphieunhap ct in dsct)
            {
                soluong += ct.soluong;
            }
            return soluong;
        }

        public void Thongketheonam_phieunhap(int nam, int[] soluongsanpham, int[] tongtien)
        {
            List<BIZ_phieunhap> ds = new List<BIZ_phieunhap>();
            ds = DAL_phieunhap.Select_phieunhap();
            List<BIZ_chitietphieunhap> dsct = new List<BIZ_chitietphieunhap>();
            dsct = DAL_phieunhap.Select_chitietphieunhap();
            dsct = (from c in dsct
                           where c.phieunhap.ngaynhap.Year == nam
                           select c).ToList();
            ds = (from pn in ds where pn.ngaynhap.Year == nam select pn).ToList();
            for (int i = 1; i < 13; i++)
            {
                soluongsanpham[i] = laysoluongsanphamtheothang(i, dsct);
                tongtien[i] = laytongtientheothang(i, ds);
            }
        }

        public int laytongtientheothang(int thang, List<BIZ_phieunhap> ds)
        {
            int tong = 0;
            ds = (from d in ds
                  where d.ngaynhap.Month == thang
                  select d).ToList();
            foreach (BIZ_phieunhap pn in ds)
            {
                    tong += pn.tongtien;
            }
            return tong;
        }

        public int laysoluongsanphamtheothang(int thang, List<BIZ_chitietphieunhap> dsct)
        {
            int soluong = 0;
            dsct = (from c in dsct
                    where c.phieunhap.ngaynhap.Month == thang
                    select c).ToList();
            foreach (BIZ_chitietphieunhap ct in dsct)
            {
                soluong += ct.soluong;
            }
            return soluong;
        }
    }
}
